Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

449
Views
Firebase Firestore returns [object Object] instead of instead of a plain object

I have a custom hook called useDocument.js below which fetches the data from a firestore collection via a specific ID. Why is it that it's returning [object Object] instead of plain object.

I've tried accessing the name property and logging it to the console using document.name but throws up an error:

Uncaught TypeError: Cannot read properties of null (reading 'name')

But when I use JSON.stringify(document), I can see the object properties and values

from documents 2: {"name":"Mark Dave Marquez","id":"D68ogPwGLCeaOfoRZIBe"}

Home.js

import React from "react";
import { Link } from "react-router-dom";
import { useAuthContext } from "../../hooks/useAuthContext";
import { useDocument } from "../../hooks/useDocument";

//components
import UserProfile from "../../components/UserProfile";

const Home = () => {
  const { document, error } = useDocument("test", "D68ogPwGLCeaOfoRZIBe");
  console.log("documents 2: " + document); 
  return <div> Test </div>
};  
export default Home;

useDocument.js

import { useEffect, useState } from "react";
import { projectFirestore } from "../firebase/config";
 
export const useDocument = (collection, id) => {
  const [document, setDocument] = useState(null);
  const [error, setError] = useState(null);
 
  // realtime document data
  useEffect(() => {
    const ref = projectFirestore.collection(collection).doc(id);
 
    const unsubscribe = ref.onSnapshot(
      (snapshot) => {
        // need to make sure the doc exists & has data
        if (snapshot.data()) {
          setDocument({ ...snapshot.data(), id: snapshot.id });
          setError(null);
        } else {
          setError("No such document exists");
        }
      },
      (err) => {
        console.log(err.message);
        setError("failed to get document");
      }
    );
 
    // unsubscribe on unmount
    return () => unsubscribe();
  }, [collection, id]);
 
  return { document, error };
};

From the collection:

From the Firestore Database

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Never mind, this is due to the way I use console.log("documents 2: " + document); I forgot that the + sign converts the object to a string, so the output would become [object Object].

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!